package com.yanqu.road.server.cmd.pageant;

import com.yanqu.road.entity.activity.ActivityInfo;
import com.yanqu.road.entity.activity.game.result.UserPageantDataResult;
import com.yanqu.road.pb.pageant.PageantProto;
import com.yanqu.road.server.cmd.base.AbstractCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.activity.game2048.PageantModule;
import com.yanqu.road.server.manger.activity.GamePageantMgr;
import com.yanqu.road.server.pb.PageantPb;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.server.protocol.GameProtocol;
import com.yanqu.road.server.protocol.Protocol;

@Cmd(code = GameProtocol.S_CROSS_PAGEAN_END, desc = "选美结束跨服返回")
public class PageantEndFromCrossCmd extends AbstractCommand {
    private UserPageantDataResult result;

    @Override
    public boolean parseData(YanQuMessage packet) throws Exception {
        return true;
    }

    @Override
    public void execute(GamePlayer player, YanQuMessage packet) throws Exception {
        PageantProto.CrossPageantEndRespMsg msg = PageantProto.CrossPageantEndRespMsg.parseFrom(packet.getBytes());
        ActivityInfo activityInfo = GamePageantMgr.getActivityInfo();
        int activityId = activityInfo.getActivityId();
        result = player.getModule(PageantModule.class).settlementBeautyActivity(activityId);
        result.setSingleRanking(msg.getSingleRank());
        result.setTotalRanking(msg.getTotalRank());
    }

    @Override
    public void sendData(GamePlayer player) {
        PageantProto.PageantSettlementBeautyRespMsg.Builder respMsg = PageantProto.PageantSettlementBeautyRespMsg.newBuilder();
        if (0 == ret) {
            respMsg.setRet(result.getRet());
            if (0 == result.getRet()) {
                if (null != result.getUserPageantData()) {
                    respMsg.setBeautyId(result.getOldBeautyId());
                    respMsg.setScore(result.getScore());
                    respMsg.setEntity(PageantPb.parsePageantBaseRespMsg(result.getUserPageantData()));
                    respMsg.setSingleRanking(result.getSingleRanking());
                    respMsg.setUpRanking(0);
                    respMsg.setTotalRanking(result.getTotalRanking());
                    respMsg.setTotalUpRanking(0);
                    respMsg.setEvaluation(result.getEvaluation());
                }
            }
        } else {
            respMsg.setRet(ret);
        }
        player.sendPacket(Protocol.U_PAGEANT_SETTLEMENT, respMsg);
    }
}
